package com.yomahub.liteflow.example.freemarker.service.impl;

import cn.hutool.core.map.MapUtil;
import com.yomahub.liteflow.example.freemarker.dto.Product;
import com.yomahub.liteflow.example.freemarker.service.TemplateService;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.*;

@Service
public class TemplateServiceImpl implements TemplateService {
    // 本地模板文件：resources/freemarker/test.ftlh
    @Override
    public String getTemplateContentFromFile(){

        Configuration cfg = new Configuration(Configuration.VERSION_2_3_31);
        cfg.setDefaultEncoding("UTF-8");
        try {
            ClassPathResource freemarker = new ClassPathResource("freemarker");
            File file = freemarker.getFile();
            cfg.setDirectoryForTemplateLoading(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        cfg.setSQLDateAndTimeTimeZone(TimeZone.getDefault());
        StringWriter writer = null;
        try {
            Template template = cfg.getTemplate("test.ftlh");
            Product product = new Product();
            product.setName("zjy");
            product.setUrl("www.baidu.com");
            HashMap<String, Object> map = new HashMap<>();
            map.put("user", "wwowowow");
            map.put("latestProduct", product);

            writer = new StringWriter();
            template.process(map, writer);
        } catch (IOException | TemplateException e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(writer);
        }
        return writer.toString();
    }

    @Override
    public String getTemplateContentFromString(String content, Map<String, Object> dataModal) {
        if (MapUtil.isEmpty(dataModal)) {
            return null;
        }
        String fileName = dataModal.getOrDefault("fileName", "demo.html").toString();
        StringReader reader = new StringReader(content);
        StringWriter writer = null;
        try {
            Template template = new Template(fileName, reader, null, "UTF-8");
            writer = new StringWriter();
            template.process(dataModal, writer);

        } catch (IOException e) {
            e.printStackTrace();
        } catch (TemplateException e) {
            e.printStackTrace();
        } finally {
            org.apache.commons.io.IOUtils.closeQuietly(writer);
        }

        return writer.toString();
    }

    @Override
    public String getTemplateContentToXml() {
        Configuration cfg = new Configuration(Configuration.VERSION_2_3_31);
        cfg.setDefaultEncoding("UTF-8");
        try {
            ClassPathResource freemarker = new ClassPathResource("freemarker");
            File file = freemarker.getFile();
            cfg.setDirectoryForTemplateLoading(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        cfg.setSQLDateAndTimeTimeZone(TimeZone.getDefault());
        StringWriter writer = null;
        try {
            Template template = cfg.getTemplate("demo.ftlh");

            Map<String, Object> dataModal = new HashMap<String, Object>();

            dataModal.put("test", "aaaa");
//            dataModal.put("test1", "bbb");
            dataModal.put("test3", "cccc");

            List<String> list = new ArrayList<>();
            list.add("ee");
            list.add("rr");
            dataModal.put("list", list);

            writer = new StringWriter();
            template.process(dataModal, writer);
        } catch (IOException | TemplateException e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(writer);
        }
        return writer.toString();
    }

    @Override
    public String getTemplateContentToXmlFile() {
        Configuration cfg = new Configuration(Configuration.VERSION_2_3_31);
        cfg.setDefaultEncoding("UTF-8");
        try {
            ClassPathResource freemarker = new ClassPathResource("freemarker");
            File file = freemarker.getFile();
            cfg.setDirectoryForTemplateLoading(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        cfg.setSQLDateAndTimeTimeZone(TimeZone.getDefault());
        StringWriter writer = null;
        try {
//            Template template = cfg.getTemplate("var-template.ftlh");
            Template template = cfg.getTemplate("var-attr-template.ftlh");

            Map<String, Object> root = new HashMap();
            File file = new ClassPathResource("freemarker/var-attr.xml").getFile();
            root.put("doc", freemarker.ext.dom.NodeModel.parse(file));

            writer = new StringWriter();
            template.process(root, writer);
        } catch (IOException | TemplateException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(writer);
        }
        return writer.toString();
    }
}
